package com.javahash.spring.service;

import java.util.List;

import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.ConnectionFactory;
import com.javahash.mybatis.dao.user.UserDao.UserMapper;
import com.javahash.spring.classes.User;

@Service
public class UserService {
	
	public List<User> add(User u)
	{	
		SqlSession sqlSession = ConnectionFactory.getSession().openSession();
		try{
			UserMapper dao = sqlSession.getMapper(UserMapper.class);
			dao.addUser(u);
			List<User> rm = dao.getAllUser();
			sqlSession.commit();
			return rm;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
		return null;
	}
	
	public List<User> delete(User u)
	{
		SqlSession sqlSession = ConnectionFactory.getSession().openSession();
		try{
			UserMapper dao = sqlSession.getMapper(UserMapper.class);
			dao.deleteUser(u);
			List<User> rm = dao.getAllUser();
			sqlSession.commit();
			return rm;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally{
			sqlSession.close();
		}
		return null;
	}
	
	public List<User> show() throws Exception
	{
		SqlSession sqlSession = ConnectionFactory.getSession().openSession();
		try{
			UserMapper dao = sqlSession.getMapper(UserMapper.class);
			List<User> rm = dao.getAllUser();
			return rm;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally{
			sqlSession.close();			
		}
		return null;
	}	

}
